package com.our123.cms.web;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class ImageViewServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String[] nodes = request.getRequestURI().split("/");
		if (nodes.length > 4 && nodes[2].equals("avatar")) {
			String url = "/upload/avatar_" + nodes[3] + "/" + nodes[4];
			File f = new File(getServletContext().getRealPath(url));
			if (f.exists()) {
				response.sendRedirect(url);
			} else {
				response.sendRedirect("/upload/avatar_" + nodes[3] + "/default.jpg");
			}
		} else {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
		}
	}
}
